﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;
using System.Windows.Controls;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCommon.Prompt;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class ExportChildWindow : EFViewCmes
    {
        public ExportChildWindow(String _dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(_dialogType);
        }
        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.ExportWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }
        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    this.fdExportDate.Value=DateTime.Today.ToString("yyyy-MM-dd");
                    this.fdTrunoverStatus.Value = "发料";
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    this.fdTrunoverStatus.Value = "发料";
                    base.Mode = OperationMode.Update;
                    break;
                default:
                    break;
            }
        }

        #region  确定/按钮操作
        /// <summary>
        ///     确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Commit_Click(object sender, RoutedEventArgs e)
        {
            if (!ValidateField())
            {
                return;
            }

            base.OnCommitButtonClick("ZMCMC0", "export", sender, e);
        }

        public bool ValidateField()
        {
            string fdNum = String.IsNullOrEmpty(this.fdNum.Value.ToString()) ? "0" : this.fdNum.GetDataValue().ToString();
            string fdActualNumber = String.IsNullOrEmpty(this.fdActualNumber.Value.ToString()) ? "0" : this.fdActualNumber.GetDataValue().ToString();
            if (Double.Parse(fdNum) == Double.Parse("0.0"))
            {
                ValidateMessagePopup("材料数量为零，无法发料！");
                return false;
            }
            if (Double.Parse(fdActualNumber) == Double.Parse("0.0"))
            {
                ValidateMessagePopup("发料数量为零，请重新填写发料数量！");
                return false;
            }
            if (Double.Parse(fdActualNumber) > Double.Parse(fdNum))
            {
                ValidateMessagePopup("实发数量不得大于材料数量！");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 验证失败，消息提示
        /// </summary>
        public void ValidateMessagePopup(string msg)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(msg);
            pop.Show(6);

            return;
        }

        /// <summary>
        ///  取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }
        #endregion
        /// <summary>
        /// customview双击赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdMaterialName_ValueChanged(object sender, Platform.Core.UI.Controls.ValueChangedEventArgs e)
        {
            if (e.NewValue == null && e.OldValue.Equals(""))
            {
                return;
            }
            EFDataField field = sender as EFDataField;
            EFCustomDataPicker picker = field.Content as EFCustomDataPicker;
            if (picker.ItemObject != null)
            {
                if (!picker.ItemObject.Equals(""))
                {
                    this.fdMaterialId.Value = picker.ItemObject.GetType().GetProperty("fdKeyId").GetValue(picker.ItemObject, null) as string;
                    this.fdTrunoverMaterialId.Value = picker.ItemObject.GetType().GetProperty("fdKeyId").GetValue(picker.ItemObject, null) as string;
                    this.fdMaterialCode.Value = picker.ItemObject.GetType().GetProperty("fdMaterialCode").GetValue(picker.ItemObject, null) as string;
                    this.fdMaterialName.Value = picker.ItemObject.GetType().GetProperty("fdMaterialName").GetValue(picker.ItemObject, null) as string;
                    this.fdMetric.Value = picker.ItemObject.GetType().GetProperty("fdMetric").GetValue(picker.ItemObject, null) as string;
                    this.fdSpecModel.Value = picker.ItemObject.GetType().GetProperty("fdSpecModel").GetValue(picker.ItemObject, null) as string;
                    this.fdNumber.Value = picker.ItemObject.GetType().GetProperty("fdNumber").GetValue(picker.ItemObject, null) as string;
                    this.fdQuality.Value = picker.ItemObject.GetType().GetProperty("fdQuality").GetValue(picker.ItemObject, null) as string;
                    this.fdNum.Value = picker.ItemObject.GetType().GetProperty("fdNum").GetValue(picker.ItemObject, null);
                    this.fdActualNumber.Value = picker.ItemObject.GetType().GetProperty("fdNum").GetValue(picker.ItemObject, null);
                    this.fdRestorePlace.Value = picker.ItemObject.GetType().GetProperty("fdRestorePlace").GetValue(picker.ItemObject, null) as string;
                }
                //没选中时赋予空字符串
                else
                {
                    this.fdMaterialCode.Value = "";
                    this.fdMaterialName.Value = "";
                    this.fdMetric.Value = "";
                    this.fdSpecModel.Value = "";
                    this.fdNumber.Value = "";
                    this.fdQuality.Value = "";
                    this.fdNum.Value = "";
                    this.fdActualNumber.Value = "";
                    this.fdRestorePlace.Value = "";
                }
            }
        }
    }
}
